//
//  CZUserAccount.swift
//  GZWeiboMing
//
//  Created by 1 on 16/4/1.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

// 对象归档需要实现NSCoding

class CZUserAccount: NSObject, NSCoding {
    //用于调用access——token，接口获得授权后的access_token
    var access_token: String?
    
    var expires_in: NSTimeInterval = 0 {
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var expiresDate: NSDate?
    //当前授权用户的UID
    var uid: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("forUndefinedKey: \(key)")
    }
    
    override var description: String {
        get {
            let keys = ["access_token","expires_in","expiresDate","uid"]
            
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expireDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
    }

}












